//
//  UPTitleCell.m
//  UESTCPioneer
//
//  Created by Sway on 14-3-5.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "UPTitleCell.h"

@interface UPTitleCell(){
    UIButton *collectButton;
    /*最新添加*/
    UIImageView * headImageView;
    NSString * _imagePath;
    /**/
}

@end

@implementation UPTitleCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        _CollectButtonEnable=NO;
        _collecting=NO;
        
        /*最新添加*/
        _HeadImageEnable = NO;
        /**/
        
        CGRect titleRect = CGRectMake(10, 10, 280, 20);
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:titleRect];
        titleLabel.font = [UIFont systemFontOfSize:titleFontSize];
        titleLabel.tag = titleTag;
        titleLabel.textColor = [UIColor blackColor];
        [titleLabel setTextAlignment:NSTextAlignmentLeft];
        [self.contentView addSubview:titleLabel];
        
        CGRect timeRect = CGRectMake(10, 30, 250, 20);
        UILabel *timeLabel = [[UILabel alloc]initWithFrame:timeRect];
        timeLabel.font = [UIFont systemFontOfSize:timeFontSize];
        timeLabel.tag = timeTag;
        timeLabel.textColor = [UIColor grayColor];
        [timeLabel setTextAlignment:NSTextAlignmentLeft];
        [self.contentView addSubview:timeLabel];
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

-(void)setTitle:(NSString*)title{
    UILabel *titleLabel = (UILabel *)[self.contentView viewWithTag:titleTag];
    titleLabel.text=title;
    titleLabel.textColor = kNavigationBarColor;
    [titleLabel setFont:[UIFont fontWithName:@"HoeflerText-BlackItalic" size:15.0f]];
}

-(void)setTitleColor:(UIColor *)color{
    UILabel *titleLabel = (UILabel *)[self.contentView viewWithTag:titleTag];
    [titleLabel setTextColor:color];
}
-(void)setTime:(NSString*)time{
    UILabel *timeLabel = (UILabel *)[self.contentView viewWithTag:timeTag];
    timeLabel.text=time;
}

-(void)setCollectButtonEnable:(BOOL)CollectButtonEnable{
    _CollectButtonEnable=CollectButtonEnable;
    if (CollectButtonEnable){
        if (collectButton!=nil)return;
        
        CGRect collectRect = CGRectMake(230, 20, 60, 20);
        UIButton *collect = [[UIButton alloc]initWithFrame:collectRect];
        collectButton=collect;
        collect.tag = collectTag;
        //        collect.hidden = YES;
        [collect setImage:[UIImage imageNamed:@"collect.png"] forState:UIControlStateNormal];
        [collect setImage:[UIImage imageNamed:@"collect_highlighted.png"] forState:UIControlStateHighlighted];
        [collect setImage:[UIImage imageNamed:@"collect_highlighted.png"] forState:UIControlStateSelected];
        [collect addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:collect];
        
    }
    else {
        [collectButton removeFromSuperview];
        collectButton=nil;
    }
}

-(void)collect:(UIButton*)button{
    button.selected=!button.selected;
    if ([self.delegate respondsToSelector:@selector(UPTitleCell:CollectButtonClick:)]){
        [self.delegate UPTitleCell:self CollectButtonClick:button];
    }
}

-(void)setCollectButtonStatus:(BOOL)status{
    if (status){
        collectButton.selected=YES;
    }
    else {
        collectButton.selected=NO;
    }
}
/*最新添加*/
-(void)setHeadImageEnable:(BOOL)HeadImageEnable{
    _HeadImageEnable=HeadImageEnable;
    if (HeadImageEnable){
        if (headImageView!=nil)return;
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 40, 40)];
        [imageView setImage:[UIImage imageNamed:@"default_head_image.png"]];
        imageView.tag = headImageTag;
        headImageView = imageView;
        [self.contentView addSubview:headImageView];
        UILabel *timeLabel = (UILabel *)[self.contentView viewWithTag:timeTag];
        UILabel *titleLabel = (UILabel *)[self.contentView viewWithTag:titleTag];
        
        CGRect rect = CGRectMake(timeLabel.frame.origin.x+40, timeLabel.frame.origin.y, timeLabel.frame.size.width, timeLabel.frame.size.height);
        [timeLabel setFrame:rect];
        
        rect = CGRectMake(titleLabel.frame.origin.x + 40, titleLabel.frame.origin.y, titleLabel.frame.size.width, titleLabel.frame.size.height);
        [titleLabel setFrame:rect];
        
        [self.contentView addSubview:titleLabel];
        [self.contentView addSubview:timeLabel];
    }
    else {
        
        [headImageView removeFromSuperview];
        headImageView = nil;
    }
}


-(void)setHeadImage:(NSString *)imagePath{
    //    UIImageView * imageView = (UIImageView *)[self.contentView viewWithTag:headImageTag];
    _imagePath = imagePath;
    if (_imagePath == nil) {
        NSLog(@"nil了。。。。。。。。。。。。");
        return;
    }
    NSLog(@"11111");
    if (_imagePath == NULL) {
        NSLog(@"NULL了。。。。。。。。。。。。");
        return;
    }
    NSLog(@"22222");
    NSLog(@"_imagePath = %@",_imagePath);
    [self performSelectorInBackground:@selector(bigTask) withObject:nil];
}

#pragma  mark 耗时操作
-(void)bigTask{
    NSLog(@"大任务：[NSThread currentThread] = %@",[NSThread currentThread]);
    
    UIImage * image = [UIImage imageWithContentsOfFile:_imagePath];
    //在主线程中修改头像
    [self performSelectorOnMainThread:@selector(changeImage:) withObject:image waitUntilDone:YES];
    //    [self.imageView  performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
}

-(void) changeImage:(UIImage *)image{
    [headImageView setImage:image];
}
/**/
@end
